package com.bana.fvs.app.enter;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.mvp.ConstantsValue;
import com.wei.commonlibrary.utils.SPUtils;
import com.wei.commonlibrary.utils.ToastUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetBaseUrlActivity extends BaseActivity {


    @BindView(R.id.et_base_url)
    EditText etBaseUrl;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_skip)
    TextView tv_skip;
    @BindView(R.id.et_base_url_port)
    EditText etBaseUrlPort;

    private int type = 0;

    @Override
    public int setLayout() {
        return R.layout.activity_set_base_url;
    }

    @Override
    public void setTitle() {
        if (getIntent().hasExtra("type")) {
            type = getIntent().getIntExtra("type", 0);
        }

        mBaseTitleBarView.setTitleText(getString(R.string.set_your_serviceurl));
        mBaseTitleBarView.setLeftGone();
    }

    @Override
    public void initWidget() {


        if(TextUtils.isEmpty(SPUtils.getBaseUrl(this))){
            etBaseUrl.setText(ConstantsValue.baseHost);
            etBaseUrlPort.setText(ConstantsValue.basePort);
        }else {
            etBaseUrl.setText(SPUtils.getBaseUrl(this));
            etBaseUrlPort.setText(SPUtils.getBaseUrlPort(this));
        }

    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }


    @OnClick({R.id.tv_sure, R.id.tv_skip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sure:
                setBaseUrl();
                break;
            case R.id.tv_skip:
                SPUtils.setBaseUrl(this, "");
                SPUtils.setBaseUrlPort(this, "");
                ToastUtil.showShort(this, "设置成功,重启App后生效！");
                finish();

                break;
        }
    }


    private void setBaseUrl() {

        String baseUrl=etBaseUrl.getText().toString();
        String port=etBaseUrlPort.getText().toString();

        if (TextUtils.isEmpty(baseUrl)) {
            ToastUtil.showShort(this, "输入ip地址为空");
        } else if (!isHttpUrl(baseUrl)) {
            ToastUtil.showShort(this, "请检查网址格式！");
        } if (TextUtils.isEmpty(port)) {
            ToastUtil.showShort(this, "输入端口为空");
        } else if (!isPort(port)) {
            ToastUtil.showShort(this, "请检查端口格式！");
        } else {

            SPUtils.setBaseUrl(this, baseUrl);
            SPUtils.setBaseUrlPort(this, port);
            if (type == 1) {
                ToastUtil.showShort(this, "设置成功,重启App后生效！");
                finish();
            } else {
                ToastUtil.showShort(this, "设置成功,重启App后生效！");
                finish();
            }
        }


    }


    public boolean isHttpUrl(String urls) {
        boolean isurl = false;
      //  String regex = "(((https|http)?://)?([a-z0-9]+[.])|(www.))" + "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)+((:\\d*)?)";//设置正则表达式
//"[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62}|(:[0-9]{1,4}))+\\\\.?";

        String regex ="((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
        Pattern pat = Pattern.compile(regex.trim());//比对
        Matcher mat = pat.matcher(urls.trim());
        isurl = mat.matches();//判断是否匹配
        if (isurl) {
            isurl = true;
        }
        return isurl;
    }

    //

    public boolean isPort(String urls) {
        boolean isurl = false;

        String regex ="([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-5]{2}[0-3][0-5])";
        Pattern pat = Pattern.compile(regex.trim());//比对
        Matcher mat = pat.matcher(urls.trim());
        isurl = mat.matches();//判断是否匹配
        if (isurl) {
            isurl = true;
        }
        return isurl;
    }

}
